package com.huluobo.a2208mdmdemo2

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.huluobo.a2208mdmdemo2.ui.theme._2208MDMDemo2Theme
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestPermissions(
            arrayOf(
                android.Manifest.permission.ACCESS_COARSE_LOCATION,
                android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                android.Manifest.permission.READ_EXTERNAL_STORAGE,
            ), 102
        )
        setContent {
            _2208MDMDemo2Theme {
                // A surface container using the 'background' color from the theme
                Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
                    var startPage = "welcome"
                    val mmkv = MMKV.defaultMMKV()
                    val navController = rememberNavController()
                    val isIn = mmkv.decodeBool("isIn", false)
                    if (isIn) {
                        startPage = "main"
                    }
//                    else {
//                        lifecycleScope.launch {
//                            async { delay(1000) }.await()
//                            navController.navigate("main")
//                            mmkv.encode("isIn", true)
//                        }
//                    }

                    NavHost(navController = navController, startDestination = startPage, builder = {
                        composable("welcome") { WelcomePage(navController) }
                        composable("main") { MainPage() }
                    })
                }
            }
        }
    }
}